package com.example.lhh.shiro;

import com.example.lhh.jwt.LhhJwtToke;
import com.example.lhh.user.entity.LhhUser;
import com.example.lhh.user.service.ILhhUserService;
import com.example.lhh.jwt.LhhJwtUtil;
import com.example.lhh.util.LhhSha256Util;
import io.jsonwebtoken.Claims;
import lombok.Data;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author luoHongHu
 * @Description 密码验证器
 * @date 2025年03月30日
 */

@Data
@Component
public class LhhCredentialsMatch extends SimpleCredentialsMatcher {

   @Resource
   ILhhUserService iLhhUserService;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        LhhJwtToke jwtToke = (LhhJwtToke)token;
        //String getToken = (String)jwtToke.getCredentials();//输入的账户
        String inPassWord = ((Arrays.toString(jwtToke.getPassword())).replace("[", "").replace("]", "").replace(",","")).replace(" ","");
        Claims claims = LhhJwtUtil.parseJwt(jwtToke.getToke());
        String userName = (String) claims.get("aud");//获取用户
        LhhUser lhhUser = iLhhUserService.getLhhUserByUserNo(userName);
        String salt = lhhUser.getSalt();
        String getPassWord = LhhSha256Util.createPassword(salt + inPassWord);
        return equals(lhhUser.getPassWord(),getPassWord);
    }
}
